package com.wwtech.epp.ui.activity.work;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.entity.Approve;
import com.wwtech.epp.entity.ApproveState;
import com.wwtech.epp.ui.activity.work.component.DaggerApproveDetailComponent;
import com.wwtech.epp.ui.activity.work.contract.ApproveDetailContract;
import com.wwtech.epp.ui.activity.work.module.ApproveDetailModule;
import com.wwtech.epp.ui.activity.work.presenter.ApproveDetailPresenter;
import com.wwtech.epp.ui.adapter.work.ApproveStateAdapter;
import com.wwtech.epp.utils.Dynamic.ApproveDesc;
import com.wwtech.epp.utils.ToastUtil;
import com.wwtech.epp.utils.WorkUtil;
import com.wwtech.epp.view.GlideCircleTransform;
import com.wwtech.epp.view.dynamicapp.ItemAppView;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: $description
 * @date 2017/09/22 09:41:14
 * 审批详情
 */

public class ApproveDetailActivity extends BaseActivity implements ApproveDetailContract.View {

    @Inject
    ApproveDetailPresenter mPresenter;
    ApproveStateAdapter approveStateAdapter;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    //    @BindView(R.id.time)
//    TextView time;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.operate)
    TextView operate;
    @BindView(R.id.state)
    TextView state;
    @BindView(R.id.sure)
    TextView sure;
    @BindView(R.id.recycerView)
    RecyclerView recycerView;
    @BindView(R.id.ll_oprete)
    LinearLayout llOprete;
    private Approve.DataBean.ListBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_approve_detail);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (getIntent().getStringExtra("type").equals("抄送我的") || getIntent().getStringExtra("type").equals("我已审批")) {
            llOprete.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        dataBean = getIntent().getParcelableExtra("databean");
        Map<String, String> map = new HashMap<>();
        map.put("reviewId", ContainValue.userLogin.getData().getUserId() + "");
        map.put("requestId", dataBean.getRequestId() + "");
        mPresenter.getApproveDetail(dataBean.getRequestId() + "", map);
        setTitle(dataBean.getNickName() + "的" + WorkUtil.idParse2Type(dataBean.getAppId()));
        tvTitle.setText(dataBean.getNickName() + "的" + WorkUtil.idParse2Type(dataBean.getAppId()));
        Glide.with(this)
                .load(dataBean.getAvatar())
                .transform(new GlideCircleTransform(this))
                .into(ivAvatar);
        WorkUtil.approveStateParse(this, dataBean.getState(), state, dataBean.getReviewerNick());
        String requestInfo = dataBean.getRequsetInfo();
        requestInfo = "{\"data\":" + requestInfo + "}";
        KLog.i(requestInfo);
        Gson gson = new Gson();
        ApproveDesc approveDesc = gson.fromJson(requestInfo, ApproveDesc.class);
        KLog.i(approveDesc.getData().size());
        for (int i = 0; i < approveDesc.getData().size(); i++) {
            if (approveDesc.getData().get(i).getKey().contains("人"))
                continue;
            KLog.i(approveDesc.getData().size());
            ItemAppView itemAppView = new ItemAppView(this);
            String empty = "";
            if (approveDesc.getData().get(i).getKey().length() < 5) {
                int length = (5 - approveDesc.getData().get(i).getKey().length()) * 4;
                for (int j = 0; j < length; j++) {
                    empty += " ";
                }
            }
            itemAppView.getTitle().setText(approveDesc.getData().get(i).getKey() + empty);
            itemAppView.getContent().setText(approveDesc.getData().get(i).getValue());
            llContent.addView(itemAppView);
        }
        if (dataBean.getUserId() == ContainValue.userLogin.getData().getUserId()) {
            if (dataBean.getState() == 0) {
                operate.setText("撤销");
            }
        } else {
            if (dataBean.getState() == 0) {
                operate.setText("拒绝");
                sure.setText("通过");
                sure.setVisibility(View.VISIBLE);
            }
        }
        if (dataBean.getState() == 1 || dataBean.getState() == 2 || dataBean.getState() == -1) {
            sure.setVisibility(View.GONE);
            operate.setVisibility(View.GONE);
        }
    }

    @Override
    protected void setupActivityComponent() {
        DaggerApproveDetailComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .approveDetailModule(new ApproveDetailModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(ApproveDetailContract.ApproveDetailContractPresenter presenter) {
        mPresenter = (ApproveDetailPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void onRemoveApproveSuccess() {
        EventBus.getDefault().post("deletApprove");
        ToastUtil.displayShortToast("撤销审批成功");
        finish();
    }

    @Override
    public void onGetApproveStateSuccess(ApproveState approveState) {
        approveStateAdapter = new ApproveStateAdapter(approveState.getData().getReviewStatus());
        recycerView.setAdapter(approveStateAdapter);
    }

    @Override
    public void onOperateSuccess() {
//        Map<String, String> map = new HashMap<>();
//        map.put("reviewId", ContainValue.userLogin.getData().getUserId() + "");
//        map.put("requestId", dataBean.getRequestId() + "");
//        mPresenter.getApproveDetail(dataBean.getRequestId() + "", map);
        ToastUtil.displayShortToast("操作成功");
        EventBus.getDefault().post("opetateApprove");
        finish();
    }

    @OnClick({R.id.operate, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.operate:
                if (dataBean.getUserId() == ContainValue.userLogin.getData().getUserId()) {
                    if (dataBean.getState() == 0) {
                        //撤销该审批
                        Map<String, String> map = new HashMap<>();
                        map.put("requestId", dataBean.getRequestId() + "");
                        map.put("userId", ContainValue.userLogin.getData().getUserId() + "");
                        mPresenter.removeApprove(map);
                    }
                } else {
                    //拒绝该审批
                    showEnterContentDialog(2);
//                    Map<String, String> map = new HashMap<>();
//                    map.put("opUser", ContainValue.userLogin.getData().getUserId() + "");          // 这里我填的是当前登录人的userId
//                    map.put("reviewId", dataBean.getReviewId() + "");        //
//                    map.put("reviewRequestId", dataBean.getRequestId() + "");                     //这里填的是审批的requestId
//                    map.put("appId", dataBean.getAppId() + "");
//                    map.put("content", "拒绝");
//                    map.put("opType", "2");                                                         //操作类型。1通过， 2拒绝， 3驳回
//                    mPresenter.operateApprove(map);
                }
                break;
            case R.id.sure:
                //通过该审批
                showEnterContentDialog(1);
//                Map<String, String> map = new HashMap<>();
//                map.put("opUser", ContainValue.userLogin.getData().getUserId() + "");          // 这里我填的是当前登录人的userId
//                map.put("reviewId", dataBean.getReviewId() + "");
//                map.put("reviewRequestId", dataBean.getRequestId() + "");                     //这里填的是审批的requestId
//                map.put("appId", dataBean.getAppId() + "");
//                map.put("content", "统一");
//                map.put("opType", "1");                                                         //操作类型。1通过， 2拒绝， 3驳回
//                mPresenter.operateApprove(map);
                break;
        }
    }

    private void showEnterContentDialog(int postion) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        View view1 = View.inflate(this, R.layout.enter_content_dialog, null);
        alertDialog.setView(view1);
        alertDialog.setCancelable(true);
        EditText editText = (EditText) view1.findViewById(R.id.content);
        TextView cancal = (TextView) view1.findViewById(R.id.cancal);
        TextView sure = (TextView) view1.findViewById(R.id.sure);
        cancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editText.getText().toString().trim().equals("")) {
                    ToastUtil.displayShortToast("请输入内容");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("opUser", ContainValue.userLogin.getData().getUserId() + "");          // 这里我填的是当前登录人的userId
                map.put("reviewId", dataBean.getReviewId() + "");
                map.put("reviewRequestId", dataBean.getRequestId() + "");                     //这里填的是审批的requestId
                map.put("appId", dataBean.getAppId() + "");
                map.put("content", editText.getText().toString().trim());
                map.put("opType", postion + "");                                                         //操作类型。1通过， 2拒绝， 3驳回
                mPresenter.operateApprove(map);
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
    }
}